package com.xmx.controller;


import com.xmx.pojo.User;
import com.xmx.service.IUserService;
import com.xmx.util.DateUtils;
import com.xmx.util.SHA256;
import com.xmx.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.awt.image.BufferedImage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jianzi
 * @since 2022-08-10
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    IUserService iUserService;

    //1.登录
    @RequestMapping("/userlogin")
    public Object userlogin(@RequestParam("username") String username,
                            @RequestParam("password") String password,
                            Model model) {
        User user = iUserService.userlogin(username, SHA256.SHA256(password));
        if (user != null) {
            List<User> list = iUserService.findAll();
            model.addAttribute("list", list);

            return "emplist";
        } else {
            return "redirect:/login.html";
        }
    }

    //2.生成普通验证码
    @RequestMapping("/getImage")
    public void getImage(HttpSession session, HttpServletResponse response) throws IOException {
        //生成验证码
        String securityCode = VerifyCodeUtils.getSecurityCode();
        //将验证码放入session
        session.setAttribute("yzm", securityCode);
        //生成图片
        BufferedImage image = VerifyCodeUtils.createImage(securityCode);
        //输出图片
        ServletOutputStream outputStream = response.getOutputStream();
        //调用工具类
        ImageIO.write(image, "png", outputStream);
    }

    //3.注册
    @RequestMapping("/userregist")
    public Object userregist(HttpSession session, User user, @RequestParam String code, @RequestParam String password) {
        String yzm = session.getAttribute("yzm") + "";
        System.out.println(code);
        //设置验证码超时时间
        session.setMaxInactiveInterval(60 * 3);
        if (code.equals(yzm)) {
            user.setPassword(SHA256.SHA256(password));
            iUserService.userregist(user);
            System.out.println("注册成功");
            session.setAttribute("user", user);
            return "redirect:/login.html";
        } else {
            System.out.println("注册失败");
            return "redirect:/register.html";
        }
    }

    //4.删除
    @RequestMapping("/delete")
    public Object delete(@RequestParam Integer uid,Model model) {
        iUserService.delete(uid);
        List<User> list = iUserService.findAll();
        model.addAttribute("list", list);
        return "emplist";
    }
    //通过uid找用户
    @RequestMapping("/findbyid")
    public Object findbyuid(@RequestParam int uid,Model model) {
        User user =  iUserService.findbyid(uid);
        model.addAttribute( "user1",user);
        return "updateEmp";
    }
    //6.保存修改
    @RequestMapping("/save")
    public Object save(@RequestParam int uid,Model model,@RequestParam String username,
                         @RequestParam String salary,@RequestParam Date bir) {
        User user = new User();
        user.setUsername(username);
        user.setSalary(salary);
        user.setBir(bir);
        iUserService.update(uid);
        List<User> list = iUserService.findAll();
        model.addAttribute("user1", list);
        return "emplist";
    }
    //7.添加
    @RequestMapping("/add")
    public Object add(@RequestParam int uid,Model model,@RequestParam String username,
                       @RequestParam String salary,@RequestParam Date bir) {
        User user = new User();
        user.setUsername(username);
        user.setSalary(salary);
        user.setBir(bir);
        iUserService.update(uid);
        List<User> list = iUserService.findAll();
        model.addAttribute("user1", list);
        return "emplist";
    }

}

